/*
 * taskTreePanel.java
 *
 * Created on August 4, 2008, 9:51 AM
 */

package jwu2.gui;

import jwu2.core.Task;
import jwu2.core.TaskListener;

/**
 *
 * @author  jbk
 */
public class TaskTreePanel extends javax.swing.JPanel implements TaskListener {
    private Task task;
    private TaskTreePanel parentTree;
    
    /** Creates new form taskTreePanel */
    public TaskTreePanel(Task task, TaskTreePanel parent) {
        this.task = task;
        this.parentTree = parent;
        task.addListener(this);
        initComponents();
        singleTaskName.setText(task.getName());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        singleTaskName = new javax.swing.JLabel();
        singleTaskProgressBar = new javax.swing.JProgressBar();
        singleTaskSubpanel = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();

        setBackground(SimpleMain.textBackground);

        singleTaskName.setText("Task");
        singleTaskName.setMinimumSize(new java.awt.Dimension(0, 15));

        singleTaskProgressBar.setString("...");
        singleTaskProgressBar.setStringPainted(true);

        singleTaskSubpanel.setBackground(SimpleMain.textBackground);
        singleTaskSubpanel.setPreferredSize(new java.awt.Dimension(100, 0));
        singleTaskSubpanel.setLayout(new javax.swing.BoxLayout(singleTaskSubpanel, javax.swing.BoxLayout.LINE_AXIS));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(singleTaskName, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
            .addComponent(singleTaskProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(singleTaskSubpanel, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(singleTaskName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(singleTaskProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(singleTaskSubpanel, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel singleTaskName;
    private javax.swing.JProgressBar singleTaskProgressBar;
    private javax.swing.JPanel singleTaskSubpanel;
    // End of variables declaration//GEN-END:variables

    public void onCompletion(Task task) {
        if (parentTree!=null){
            parentTree.removeChild(this);
        }
    }

    public void onNewSubTask(Task task, Task subTask) {
        singleTaskSubpanel.add(new TaskTreePanel(subTask, this));
    }

    public void onProgressUpdate(Task task, Task origin) {
        int progress = (int)(task.getProgress()*(float)100);
        singleTaskProgressBar.setValue(progress);
        singleTaskProgressBar.setString(progress+"% "+task.getInfoText());
    }

    /**
     * Remove a child tree from this component
     */
    private void removeChild(TaskTreePanel child) {
        singleTaskSubpanel.remove(child);
        validate();
        repaint();
    }

    public void onInactive(Task task) {
        singleTaskProgressBar.setString("Idle");
    }

    public void onLog(Task task, String logMessage) {
        // Do nothing
    }

}
